/*
 * @Date: 2023-03-21 16:32:10
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-21 21:30:02
 * @FilePath: \A19\frontend\src\flowNodes\encry.ts
 */
import { EncryptUtil } from "@/shared/EncryptUtil";
import { ServiceType } from "@/shared/protocols/serviceProto"
import { HttpClient, WsClient } from "tsrpc-browser"

// const secret = "妄想☆ふぇてぃっしゅ!";
// const encoder = new TextEncoder()
// console.log(encoder.encode(secret));

export function encry(client: HttpClient<ServiceType>) {
   // 发送前加密
    client.flows.preSendDataFlow.push(v => {
        if(v.data instanceof Uint8Array){
            v.data = EncryptUtil.encrypt(v.data);
        }
        return v;
    });
    // 接收前解密
    client.flows.preRecvDataFlow.push(v => {
        if(v.data instanceof Uint8Array){
            v.data = EncryptUtil.decrypt(v.data);
        }
        return v;
    })
}

export function encryWs(wsClient:WsClient<ServiceType>){
    wsClient.flows.preSendDataFlow.push(v =>{
        if(v.data instanceof Uint8Array){
            v.data = EncryptUtil.encrypt(v.data);
        }
        return v
    })

    wsClient.flows.preRecvDataFlow.push(v =>{
        if(v.data instanceof Uint8Array){
            v.data = EncryptUtil.decrypt(v.data);
        }
        return v
    })
}